home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / bfd / rs6000-core.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-02  |  8.6 KB  |  265 lines

  1. /* IBM RS/6000 "XCOFF" back-end for BFD.
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.    Written by Metin G. Ozisik, Mimi Ph√⌠ng-Thσo V⌡, and John Gilmore.
  4.    Archive support from Damon A. Permezel.
  5.    Contributed by IBM Corporation and Cygnus Support.
  6.  
  7. This file is part of BFD, the Binary File Descriptor library.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. /* This port currently only handles reading object files, except when
  24.    compiled on an RS/6000 host.  -- no archive support, no core files.
  25.    In all cases, it does not support writing.
  26.  
  27.    FIXMEmgo comments are left from Metin Ozisik's original port.
  28.  
  29.    This is in a separate file from coff-rs6000.c, because it includes
  30.    system include files that conflict with coff/rs6000.h.
  31.   */
  32.  
  33. /* Internalcoff.h and coffcode.h modify themselves based on this flag.  */
  34. #define RS6000COFF_C 1
  35.  
  36. #include "bfd.h"
  37. #include "sysdep.h"
  38. #include "libbfd.h"
  39.  
  40. #ifdef    COREFILES_PLEASE
  41.  
  42. /* AOUTHDR is defined by the above.  We need another defn of it, from the
  43.    system include files.  Punt the old one and get us a new name for the
  44.    typedef in the system include files.  */
  45. #ifdef AOUTHDR
  46. #undef AOUTHDR
  47. #endif
  48. #define    AOUTHDR    second_AOUTHDR
  49.  
  50. #undef    SCNHDR
  51.  
  52.  
  53. /* ------------------------------------------------------------------------ */
  54. /*    Support for core file stuff..                         */
  55. /* ------------------------------------------------------------------------ */
  56.  
  57. #include <sys/user.h>
  58. #include <sys/ldr.h>
  59. #include <sys/core.h>
  60.  
  61.  
  62. /* Number of special purpose registers supported by gdb.  This value
  63.    should match `tm.h' in gdb directory.  Clean this mess up and use
  64.    the macros in sys/reg.h.  FIXMEmgo. */
  65.  
  66. #define    NUM_OF_SPEC_REGS  7
  67. #define    STACK_END_ADDR 0x2ff80000
  68.  
  69. #define    core_hdr(bfd)        (((Rs6kCorData*)(bfd->tdata.any))->hdr)
  70. #define    core_datasec(bfd)    (((Rs6kCorData*)(bfd->tdata.any))->data_section)
  71. #define    core_stacksec(bfd)    (((Rs6kCorData*)(bfd->tdata.any))->stack_section)
  72. #define    core_regsec(bfd)    (((Rs6kCorData*)(bfd->tdata.any))->reg_section)
  73. #define    core_reg2sec(bfd)    (((Rs6kCorData*)(bfd->tdata.any))->reg2_section)
  74.  
  75. /* These are stored in the bfd's tdata */
  76. typedef struct {
  77.   struct core *hdr;        /* core file header */
  78.   asection *data_section,
  79.          *stack_section,
  80.        *reg_section,    /* section for GPRs and special registers. */
  81.        *reg2_section;    /* section for FPRs. */
  82. } Rs6kCorData;
  83.  
  84.  
  85. /* Decide if a given bfd represents a `core' file or not. There really is no
  86.    magic number or anything like, in rs6000coff. */
  87.  
  88. bfd_target *
  89. rs6000coff_core_p (abfd)
  90.      bfd *abfd;
  91. {
  92.   int fd;
  93.   struct core_dump coredata;
  94.   struct stat statbuf;
  95.   char *tmpptr;
  96.  
  97.   /* Use bfd_xxx routines, rather than O/S primitives to read coredata. FIXMEmgo */
  98.   fd = open (abfd->filename, O_RDONLY);
  99.  
  100.   fstat (fd, &statbuf);
  101.   read (fd, &coredata, sizeof (struct core_dump));
  102.  
  103.   close (fd);
  104.  
  105.   if (coredata.c_tab < (sizeof (coredata.c_u) + (int)&coredata.c_u - (int)&coredata.c_signo) ||
  106.       coredata.c_tab >= statbuf.st_size ||
  107.       (long)coredata.c_stack <= (long)coredata.c_tab ) {
  108.     return NULL;
  109.   }
  110.  
  111. /*
  112.   If it looks like core file, then.....
  113.   read core file header..... (maybe you've done it above..)
  114. */
  115.  
  116.   /* maybe you should alloc space for the whole core chunk over here!! FIXMEmgo */
  117.   tmpptr = (char*)bfd_zalloc (abfd, sizeof (Rs6kCorData));
  118.   set_tdata (abfd, tmpptr);
  119.  
  120.   /* .stack section. */
  121.   if ((core_stacksec (abfd) = (asection*) bfd_zalloc (abfd, sizeof (asection)))
  122.        == NULL)  {
  123.     bfd_error = no_memory;
  124.     /* bfd_release (abfd, ???? ) */
  125.     return NULL;
  126.   }
  127.   core_stacksec (abfd)->name = ".stack";
  128.   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
  129.   core_stacksec (abfd)->_raw_size = coredata.c_size;
  130.   core_stacksec (abfd)->vma = STACK_END_ADDR - coredata.c_size;
  131.   core_stacksec (abfd)->filepos = coredata.c_stack;    /*???? */
  132.  
  133.   /* .reg section for GPRs and special registers. */
  134.   if ((core_regsec (abfd) = (asection*) bfd_zalloc (abfd, sizeof (asection)))
  135.        == NULL)  {
  136.     bfd_error = no_memory;
  137.     /* bfd_release (abfd, ???? ) */
  138.     return NULL;
  139.   }
  140.   core_regsec (abfd)->name = ".reg";
  141.   core_regsec (abfd)->flags = SEC_ALLOC;
  142.   core_regsec (abfd)->_raw_size = (32 + NUM_OF_SPEC_REGS) * 4;
  143.   core_regsec (abfd)->vma = NULL;            /* not used?? */
  144.   core_regsec (abfd)->filepos = 
  145.       (char*)&coredata.c_u.u_save - (char*)&coredata;
  146.  
  147.   /* .reg2 section for FPRs (floating point registers). */
  148.   if ((core_reg2sec (abfd) = (asection*) bfd_zalloc (abfd, sizeof (asection)))
  149.        == NULL)  {
  150.     bfd_error = no_memory;
  151.     /* bfd_release (abfd, ???? ) */
  152.     return NULL;
  153.   }
  154.   core_reg2sec (abfd)->name = ".reg2";
  155.   core_reg2sec (abfd)->flags = SEC_ALLOC;
  156.   core_reg2sec (abfd)->_raw_size = 8 * 32;            /* 32 FPRs. */
  157.   core_reg2sec (abfd)->vma = NULL;            /* not used?? */
  158.   core_reg2sec (abfd)->filepos = 
  159.       (char*)&coredata.c_u.u_save.fpr[0] - (char*)&coredata;
  160.  
  161.   /* set up section chain here. */
  162.   abfd->section_count = 3;
  163.   abfd->sections = core_stacksec (abfd);
  164.   core_stacksec (abfd)->next = core_regsec(abfd);
  165.   core_regsec (abfd)->next = core_reg2sec (abfd);
  166.   core_reg2sec (abfd)->next = NULL;
  167.  
  168.   return abfd->xvec;                /* this is garbage for now. */
  169. }
  170.  
  171.  
  172.  
  173. /* return `true' if given core is from the given executable.. */
  174. boolean
  175. rs6000coff_core_file_matches_executable_p (core_bfd, exec_bfd)
  176.      bfd *core_bfd;
  177.      bfd *exec_bfd;
  178. {
  179.   FILE *fd;
  180.   struct core_dump coredata;
  181.   struct ld_info ldinfo;
  182.   char pathname [1024];
  183.   char *str1, *str2;
  184.  
  185.   /* Use bfd_xxx routines, rather than O/S primitives, do error checking!!
  186.                                   FIXMEmgo */
  187.   fd = fopen (core_bfd->filename, FOPEN_RB);
  188.  
  189.   fread (&coredata, sizeof (struct core_dump), 1, fd);
  190.   fseek (fd, (long)coredata.c_tab, 0);
  191.   fread (&ldinfo, (char*)&ldinfo.ldinfo_filename[0] - (char*)&ldinfo.ldinfo_next,
  192.      1, fd);
  193.   fscanf (fd, "%s", pathname);
  194.   printf ("path: %s\n", pathname);
  195.   
  196.   str1 = strrchr (pathname, '/');
  197.   str2 = strrchr (exec_bfd->filename, '/');
  198.  
  199.   /* step over character '/' */
  200.   str1 = str1 ? str1+1 : &pathname[0];
  201.   str2 = str2 ? str2+1 : exec_bfd->filename;
  202.  
  203.   fclose (fd);
  204.   return strcmp (str1, str2) == 0;
  205. }
  206.  
  207.  
  208. boolean
  209. rs6000coff_get_section_contents (abfd, section, location, offset, count)
  210.      bfd *abfd;
  211.      sec_ptr section;
  212.      PTR location;
  213.      file_ptr offset;
  214.      int count;
  215. {
  216.     if (count == 0)
  217.     return true;
  218.  
  219.     /* Reading a core file's sections will be slightly different. For the
  220.        rest of them we can use bfd_generic_get_section_contents () I suppose. */
  221.     /* Make sure this routine works for any bfd and any section. FIXMEmgo. */
  222.  
  223.     if (abfd->format == bfd_core && strcmp (section->name, ".reg") == 0) {
  224.  
  225.       struct mstsave mstatus;
  226.       int    regoffset = (char*)&mstatus.gpr[0] - (char*)&mstatus;
  227.  
  228.       /* Assert that the only way this code will be executed is reading the
  229.          whole section. */
  230.       if (offset || count != (sizeof(mstatus.gpr) + (4 * NUM_OF_SPEC_REGS)))
  231.         printf ("ERROR! in rs6000coff_get_section_contents()\n");
  232.  
  233.       /* for `.reg' section, `filepos' is a pointer to the `mstsave' structure
  234.          in the core file. */
  235.  
  236.       /* read GPR's into the location. */
  237.       if ( bfd_seek(abfd, section->filepos + regoffset, SEEK_SET) == -1
  238.     || bfd_read(location, sizeof (mstatus.gpr), 1, abfd) != sizeof (mstatus.gpr))
  239.     return (false); /* on error */
  240.  
  241.       /* increment location to the beginning of special registers in the section,
  242.          reset register offset value to the beginning of first special register
  243.      in mstsave structure, and read special registers. */
  244.  
  245.       location = (PTR) ((char*)location + sizeof (mstatus.gpr));
  246.       regoffset = (char*)&mstatus.iar - (char*)&mstatus;
  247.  
  248.       if ( bfd_seek(abfd, section->filepos + regoffset, SEEK_SET) == -1
  249.     || bfd_read(location, 4 * NUM_OF_SPEC_REGS, 1, abfd) != 
  250.                             4 * NUM_OF_SPEC_REGS)
  251.     return (false); /* on error */
  252.       
  253.       /* increment location address, and read the special registers.. */
  254.       /* FIXMEmgo */
  255.       return (true);
  256.     }
  257.  
  258.     /* else, use default bfd section content transfer. */
  259.     else
  260.       return bfd_generic_get_section_contents 
  261.                   (abfd, section, location, offset, count);
  262. }
  263.  
  264. #endif /* COREFILES_PLEASE */
  265.